package a51qindian.com.comprehensive.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.li.interfaces.OnItemClickListener;
import com.li.utils.ToastUtil;
import com.li.widget.DoubleTextView;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import a51qindian.com.comprehensive.Const;
import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.city.CityShopAdapter;
import a51qindian.com.comprehensive.items.LinearItemDecoration;
import a51qindian.com.comprehensive.model.AddressInfo;
import a51qindian.com.comprehensive.model.BaseModel;
import a51qindian.com.comprehensive.model.CountBaseModel;
import a51qindian.com.comprehensive.model.ShopInfo;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import a51qindian.com.comprehensive.util.PopCondition;
import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description: 店铺管理
 */

public class CityShopManagerActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_shops)
    RecyclerView mRecyclerShops;

    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;
    @BindView(R.id.layout_refresh)
    SmartRefreshLayout mLayoutRefresh;
    @BindView(R.id.dtv_num)
    DoubleTextView mDtvNum;
    @BindView(R.id.pop_condition)
    PopCondition mPopCondition;
    private CityShopAdapter mCityShopAdapter;
    private List<ShopInfo> mShopInfoList;
    private String mTownId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_city_shop_manager;
    }


    @Override
    protected void initSth() {
        super.initSth();
        mShopInfoList = new ArrayList<>();
        mTownId = "";
        mTopNavigation.setNavigationBarListener(this);
        mCityShopAdapter = new CityShopAdapter(this);
        mRecyclerShops.setLayoutManager(new LinearLayoutManager(CityShopManagerActivity.this));
        mRecyclerShops.setAdapter(mCityShopAdapter);
        mRecyclerShops.addItemDecoration(new LinearItemDecoration());
        mCityShopAdapter.setItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int pos) {
                Intent intent = new Intent(CityShopManagerActivity.this, CityShopDetailActivity.class);
                intent.putExtra("uid", SpUtil.getUserInfo(CityShopManagerActivity.this, SpUtil.USER_ID));
                intent.putExtra("shop_id", mShopInfoList.get(pos).getShop_id());
                startActivity(intent);
            }
        });
        setSmartRefreshLayout(mLayoutRefresh);
        mPopCondition.setOnBack(new PopCondition.OnBack() {
            @Override
            public void onBack() {
                mIsRefresh=true;
                mShopInfoList.clear();
                mCityShopAdapter.setShopInfoList(mShopInfoList);
                mDtvNum.setTxtRight("0");
                mTownId=mPopCondition.getUId();
                showShopList(mUid, "0", mTownId);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        mPage = 0;
        mIsRefresh = true;
        getAddressList(mUid);
    }

    @Override
    public void onFunctionImgListener(View view) {
        if (mPopCondition.getPop()!=null&&mPopCondition.getPop().isShowing()) {
            mPopCondition.getPop().dismiss();
        }
        Intent intent = new Intent(this, CityAddShopActivity.class);
        startActivity(intent);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        showShopList(mUid, "0", mTownId);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        showShopList(mUid, String.valueOf(mPage + 1), mTownId);
    }


    /**
     * 显示商铺列表
     *
     * @param uid  用户编号
     * @param page 页数
     */
    private void showShopList(String uid, String page, String townId) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("page", page);
        map.put("type", "0");
        map.put("town_id", townId);
        mLayoutContent.onLoadingWithContent();
        ObservableHelper.createObservable(RetrofitCreator.createService().shopList(map))
                .subscribe(new SelectorObserver<CountBaseModel<List<ShopInfo>>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        stopRefreshLoad();
                        mLayoutRefresh.setLoadmoreFinished(true);
                        ToastUtil.showToast(CityShopManagerActivity.this, info);
                    }

                    @Override
                    protected void onSuccess(CountBaseModel<List<ShopInfo>> listBaseModel) {
                        mLayoutContent.onSuccess();
                        if (mIsRefresh) {
                            mPage = 0;
                            mShopInfoList.clear();
                            mLayoutRefresh.setLoadmoreFinished(false);
                        } else {
                            mPage++;
                        }
                        stopRefreshLoad();
                        List<ShopInfo> shopInfoList = listBaseModel.getData();
                        if (!mIsRefresh && shopInfoList.size() < Const.DEFAULT_PAGE_SIZE) {
                            mLayoutRefresh.setLoadmoreFinished(true);
                        }
                        mShopInfoList.addAll(shopInfoList);
                        mCityShopAdapter.setShopInfoList(mShopInfoList);
                        mDtvNum.setTxtRight(listBaseModel.getCount());
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        stopRefreshLoad();
                        ToastUtil.showToast(CityShopManagerActivity.this, e.getMessage());
                    }
                });
    }

    private void getAddressList(String uid) {
        mLayoutContent.onLoadingWithContent();
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        ObservableHelper.createObservable(RetrofitCreator.createService().getAddressList(map))
                .subscribe(new SelectorObserver<BaseModel<List<AddressInfo>>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseModel<List<AddressInfo>> listBaseModel) {
                        mLayoutContent.onSuccess();
                        AddressInfo addressInfo=new AddressInfo();
                        addressInfo.setUid("");
                        addressInfo.setName("全部商铺");
                        listBaseModel.getData().add(0,addressInfo);
                        mPopCondition.setData(listBaseModel.getData());
                        mTownId = mPopCondition.getUId();

                        showShopList(mUid, "0", mTownId);
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mLayoutContent.onSuccess();
                        showToast(e.getMessage());
                    }
                });
    }

    @OnClick({R.id.pop_condition, R.id.dtv_num})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pop_condition:
                mPopCondition.showPop(view);
                break;
            case R.id.dtv_num:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (mPopCondition.getPop()!=null&&mPopCondition.getPop().isShowing()) {
            mPopCondition.getPop().dismiss();
            return;
        }
        super.onBackPressed();

    }
}
